<?php
// gestion des gymnases du club

/**
 * Liste des gymnases du club
 */
function gym_get_list()
{
	$sql = 'SELECT g.id, g.name, g.address, g.postal_code, g.city,
	        g.googlemap_url, g.description
	        FROM Gymnasium g
	        ORDER BY g.name;';
	$data = sql_query($sql);
	return $data;
}


/**
 * Obtenir un gymnase par son id
 */
function gym_get_by_id($id)
{
	$id = intval($id);
	$sql = 'SELECT g.name, g.address, g.postal_code, g.city, g.googlemap_url,
	        g.description
	        FROM Gymnasium g
	        WHERE g.id = '.$id.';';
	$data = sql_query($sql);
	return mysql_fetch_assoc($data);
}

/**
 * Obtenir le nom d'un gymnase par son id
 */
function gym_get_name($id)
{
	$id = intval($id);
	$sql = 'SELECT g.name
	        FROM Gymnasium g
	        WHERE g.id = '.$id.';';
	$data = mysql_fetch_assoc(sql_query($sql));
	return $data['name'];
}

/**
 * Mise à jour d'un gymnase
 */
function gym_update($id, $name, $address, $postal_code, $city, $description, $googlemap)
{
	$id = intval($id);
	$name = mysql_real_escape_string($name);
	$address = mysql_real_escape_string($address);
	$postal_code = mysql_real_escape_string($postal_code);
	$city = mysql_real_escape_string($city);
	$description = mysql_real_escape_string($description);
	$googlemap = mysql_real_escape_string($googlemap);

	$sql = 'UPDATE Gymnasium
			SET name = "'.$name.'",
			    address = "'.$address.'",
			    postal_code = "'.$postal_code.'",
			    city = "'.$city.'",
			    description = "'.$description.'",
			    googlemap_url = "'.$googlemap.'"
	        WHERE id = '.$id.';';
	sql_query($sql);
}

/**
 * Nombre d'adhérents par gymnase
 */
function gym_count_members()
{
	$sql = 'SELECT g.id, g.name, COUNT(m.id) AS count_member
	        FROM Gymnasium g
	        LEFT JOIN Member m ON m.idGymnasium = g.id
			LEFT JOIN MemberStatus s ON s.id = m.status_id
	        WHERE s.code = "MEMBER"
	        GROUP BY g.id, g.name
	        ORDER BY g.name';
	$data = sql_query($sql);
	return $data;
}
